﻿Public Class PacketHandler

    Public handler As Handler
    Public Shared auth As Integer

    Public Sub New(ByVal handler As Handler)
        Me.handler = handler
    End Sub

    Public Sub handlePacket(ByVal buf() As Byte, ByVal index As Integer)
        Dim header As Integer = buf(index)
        Select Case header
            Case &HFF
                Dim header2 As Integer = buf(index + 4)
                Select Case header2
                    'Case &HB
                    '    doAuth(buf, index + 4 + 4)
                    '    Exit Sub
                    'Case &H14
                    '    doPing(buf, index + 4 + 4)
                    '    Exit Sub
                    Case &HF6
                        handleLogin(buf, index + 4 + 4)
                        Exit Sub
                    Case &HF4
                        handleCreate(buf, index + 4 + 4)
                        Exit Sub
                    Case &HF5
                        handleDelete(buf, index + 4 + 4)
                        Exit Sub
                    Case Else
                        log.log(LogType.MSG_RECV)
                        For i As Integer = 0 To UBound(buf) - 1 Step i + 1
                            Console.Write("{0:x2} ", buf(i + index))
                        Next
                        Console.WriteLine()
                        Exit Sub
                End Select
            Case Else
                log.log(LogType.MSG_RECV)
                For i As Integer = 0 To UBound(buf) - 1 Step i + 1
                    Console.Write("{0:x2} ", buf(i + index))
                Next
                Console.WriteLine()
                Exit Select
        End Select
    End Sub

    Public Sub handleLogin(ByVal buf() As Byte, ByVal index As Integer)
        Dim offset = index
        Dim client = PacketCreator.getString(buf, offset)
        auth = PacketCreator.getInt(buf, offset)
        Dim user = PacketCreator.getString(buf, offset)
        Dim pass = PacketCreator.getString(buf, offset)
        'offset += 4
        'Dim channel = PacketCreator.getInt(buf, offset)
        'If ISC.channels.Contains(channel) <> True Then
        '    handler.workSocket.Close()
        '    Exit Sub
        'End If
        Dim userid As Integer = -1
        If (MySQL.testLogin(user, pass, userid)) Then
            If (userid <> -1) Then
                'Packets.sendIP(Me, channel)
                Packets.charDisplay(handler, userid) 'Success
                Exit Sub
            Else
                handler.workSocket.Close()
            End If
        Else
            handler.workSocket.Close()
        End If
    End Sub

    Public Sub handleCreate(ByVal buf() As Byte, ByVal index As Integer)
        Dim offset = index

        Dim user = PacketCreator.getString(buf, offset)
        Dim pass = PacketCreator.getString(buf, offset)
        Dim slot = PacketCreator.getByte(buf, offset)
        Dim name = PacketCreator.getString(buf, offset)
        Dim userid As Integer = -1
        If MySQL.testLogin(user, pass, userid) Then
            If userid <> -1 Then
                offset += 3
                Dim hair = PacketCreator.getByte(buf, offset)
                Dim haircolor = PacketCreator.getInt(buf, offset)
                Dim gender = PacketCreator.getByte(buf, offset)
                offset += 1
                Dim face = PacketCreator.getByte(buf, offset)
                If MySQL.createChar(userid, name, slot, hair, haircolor, gender, face) Then
                    createEquips(gender, name)
                    Packets.charDisplay(handler, userid)
                Else
                    Packets.charDisplay(handler, userid)
                    Packets.loginError(handler, &H524)
                End If
                Exit Sub
            End If
        Else
            handler.workSocket.Close()
        End If
    End Sub

    Public Sub handleDelete(ByVal buf() As Byte, ByVal index As Integer)
        Dim offset = index

        Dim user = PacketCreator.getString(buf, offset)
        Dim pass = PacketCreator.getString(buf, offset)
        Dim unknown = PacketCreator.getString(buf, offset)
        Dim charid = PacketCreator.getInt(buf, offset)
        Dim userid As Integer = -1
        If MySQL.testLogin(user, pass, userid) Then
            If userid <> -1 Then
                MySQL.deleteChar(charid)
                Packets.charDisplay(handler, userid)
                Exit Sub
            Else
                handler.workSocket.Close()
            End If
        Else
            handler.workSocket.Close()
        End If
    End Sub

    Public Sub createEquips(ByVal gender As Integer, ByVal charname As String)
        Dim charid = MySQL.getCharID(charname)
        If charid <> -1 Then
            If gender = 1 Then 'Female
                MySQL.createEquip(charid, 504)
                MySQL.createEquip(charid, 508)
                MySQL.createEquip(charid, 512)
            Else 'Male
                MySQL.createEquip(charid, 502)
                MySQL.createEquip(charid, 506)
                MySQL.createEquip(charid, 510)
            End If
            MySQL.createEquip(charid, 21)
        End If
    End Sub

    Public Sub doAuth(ByVal buf() As Byte, ByVal index As Integer)
        auth = PacketCreator.getInt(buf, index)
        Packets.sendAuth(handler, auth)
    End Sub

    Public Sub doPing(ByVal buf() As Byte, ByVal index As Integer)
        Dim ping = PacketCreator.getInt(buf, index)
        Packets.sendPing(handler, ping)
    End Sub

End Class
